package cn.itcast.reentrantlock;

import cn.itcast.test1.util.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

@Slf4j(topic = "c.Demo02")
public class Demo02 {
    private static final ReentrantLock LOCK = new ReentrantLock();
    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            try {
                LOCK.lockInterruptibly();
            } catch (InterruptedException e) {
                log.debug("t1等待锁的过程被打断");
                return;
            }

            try {
                log.debug("t1获取到锁了");
            }finally {
                LOCK.unlock();
            }
        });

        LOCK.lock();
        log.debug("主线程获得到锁");
        t1.start();
        try {
            Sleeper.sleep(1);
            t1.interrupt();
        }finally {
            LOCK.unlock();
            log.debug("主线程释放锁");
        }

    }
}
